package com.fjnu.dao.common;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

public class BaseDaoImpl<T> implements BaseDao<T>{
	
	Class<T> clazz;	//T的具体类
	
	/**
	 * 通过构造方法指定DAO的具体实现类
	 */
	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {
		//ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
	    //clazz = (Class<T>) type.getActualTypeArguments()[0];
		//System.out.println("DAO的真实实现类是："+this.clazz.getName());
	    
	    Type type = getClass().getGenericSuperclass();
	    if(!(type instanceof ParameterizedType)){
	        type = getClass().getSuperclass().getGenericSuperclass();
	    }
	    clazz = (Class<T>)((ParameterizedType)type).getActualTypeArguments()[0];
     }

	/**
	 * 获取当前工作的Session
	 */
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	
	@Override
	public void save(Object entity) {
			Session session = this.getSession();
			session.save(entity);
		
	}

	@Override
	public void update(Object entity) {
	
			Session session = this.getSession();
			session.update(entity);
	}

	@Override

	public void partUpdate(int id, String[] names, Object... values) {
			Session session = this.getSession();
			String tab = clazz.getSimpleName();
			//String tab="mainInstrument";
			String hql = "update "+tab+" t";
			for(int i = 0;i < names.length;i ++){
				hql += " set t."+names[i]+"=?";
			}
			hql += " where t.id="+id;
			Query query = session.createQuery(hql);
			for(int i = 0;i < values.length;i ++){
				query.setParameter(i, values[i]);
			}
			//System.out.println("部分更新："+hql);
			query.executeUpdate();
	}


	@Override
	public void delete(Serializable id) {
		/*
		T obj = findById(id);
		Session session = this.getSession();
	    session.delete(obj);
        */
		Session session = this.getSession();
		String tab = clazz.getSimpleName();
		String hql = "delete from "+tab+" t where t.id="+id;
		Query query = session.createQuery(hql);
		query.executeUpdate();
		//System.out.println("已删除666");

	}
	

	@SuppressWarnings("unchecked")
	@Override
	public T findById(Serializable id) {
		Session session = this.getSession();
		T obj = (T) session.get(clazz, id);
		return obj;
	}

	
	public Object[] findPropertyById(Serializable id){
		
		//throw new DaoException("数据库操作异常，请稍后重试！");
		return null;
	}

	@Override
	public List<T> findByHQL(String hql, Object... params) {
		List<T> list = null;
		Session session = null;
			session = this.getSession();
			Query query = session.createQuery(hql);
			for(int i = 0;params != null && i < params.length; i ++){
				query.setParameter(i, params[i]);
			}
			System.out.println("HQL查询："+hql);
			list = query.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<T> queryPage(String hql, int pageNo, int pageSize){
		List<T> list = null;
		Session session = null;
			session = this.getSession();
			
			Query query = session.createQuery(hql);
			list = query.setFirstResult((pageNo - 1) * pageSize)
						.setMaxResults(pageSize)
						.list();
		return list;
	}

}

